#include "Data.h"
#include "fs_algo.h"

#include "Extractor.h"
#include "GlobalExtractor.h"
#include "ResourceExtractor.h"


void output_features(ofstream& ofs, const string& tag, const vector<string>& features)
{
	ofs << tag << " ";

	for(int i=0; i<(int)features.size(); i++)
	{
		ofs << features[i] << " ";
	}

	ofs << endl;
}


void init(const string &sent, vector<string>& words, vector<string>& forms, vector<string>& tags)
{
	words.clear();
	forms.clear();
	tags.clear();

	istringstream iss(sent);
	string token,word,form,tag;

	while( iss >> token )
	{
		int split = token.rfind("/");

		word = token.substr(0,split);
		tag  = token.substr(split+1,token.length());
		form = former->form(word);

		if(morph != NULL)
			morph->reform(form);

		words.push_back(word);
		forms.push_back(form);
		tags.push_back(tag);
	}
}


int main(int argc,char** argv)
{
	if(argc != 6) 
	{
		cerr << "Usage : ./this training-data feature-set is-global-f resource-dir morph-path" << endl;
		exit(1);
	}

	ifstream ifs(argv[1]);
	ofstream ofs(argv[2]);

	Extractor extractor;
	GlobalExtractor* ge = NULL;
	ResourceExtractor* re = NULL;

	if(strcmp(argv[3],"true") == 0)
	{
		ge = new GlobalExtractor();
	}
	if(strcmp(argv[4],"NONE") != 0)
	{
		re = new ResourceExtractor(argv[4]);
	}
	if(strcmp(argv[5],"NONE") != 0)
	{
		morph = new CMorphology(argv[5]);
	}

	enumer = new Enumerable();
	former = new Former();

	string line;
	int count = 0;

	while(getline(ifs,line))
	{
		vector<string> words;
		vector<string> forms;
		vector<string> tags;

		init(line,words,forms,tags);

		vector<vector<string> > features_res;
		if(re != NULL)
			re->gaze_extract(forms,features_res);

		for(int i=0; i<(int)words.size(); i++)
		{
			vector<string> features;

			// baseline feature
			extractor.extract(forms,i,features);
			//extractor.extract_extra(forms,i,features);

            /*
			// tag feature
			string t_2 = (i>=2) ? tags[i-2] : "<s>";
			string t_1 = (i>=1) ? tags[i-1] : "<s>";
			extractor.extract_Tag(forms,i,t_2,t_1,features);
            */

			// resource feature
			if(re != NULL)
				features.insert(features.end(),features_res[i].begin(),features_res[i].end());
			
			output_features(ofs,tags[i],features);
		}

		count++;
		if(count%100==0) cout << count << endl;
	}

	ifs.close();
	ofs.close();

	cerr << "Extract finished" << endl;

	return 0;
}
